define([], function () {
    function ViewModel(/*{icon:,iconWidth:,lineHeight:,pointX:,pointY:,text:,xOffset:,yOffset:,record:,onClick}*/params) {
        //$.extend(this, params);
        this._icon = params.icon;
        this._iconWidth = params.iconWidth;
        this._point =  new BMap.Point(params.pointX,params.pointY);
        this._lineHeight = params.lineHeight;
        this._text = params.text;
        this._xOffset = params.xOffset?params.xOffset:0;
        this._yOffset = params.yOffset?params.yOffset:0;
        this._record = params.record;
        this._onClick = params.onClick;
    }

    ViewModel.prototype = new BMap.Overlay();
    ViewModel.prototype.initialize = function (map) {
        var scope = this;
        this._map = map;
        var div = this._div = document.createElement("div");

        div.style.position = "absolute";
        div.style.color = 'white';
        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        div.style.textAlign = 'center';
        div.style.lineHeight = '{0}px'.formatTpl(this._lineHeight);
        div.style.visibility = 'hidden';
        div.style.cursor = 'pointer';

        var childDiv = document.createElement("div");

        childDiv.style.position = 'relative';
        div.appendChild(childDiv);

        this._img = document.createElement('img');
        this._img.src = this._icon;
        this._img.style.display = 'block';
        if(this._iconWidth) this._img.style['width'] = '{0}px'.formatTpl(this._iconWidth);
        childDiv.appendChild(this._img);

        var textDiv = document.createElement('div');

        textDiv.innerText = this._text?this._text:'';
        textDiv.style.position = 'absolute';
        textDiv.style.top = '0';
        textDiv.style.left ='0';
        childDiv.appendChild(textDiv);

        this._img.onload = function(){
            textDiv.style.width = '{0}px'.formatTpl(scope._img.width);
            div.style.visibility = 'visible';
            scope.draw();

            if(scope._onClick) {
                scope._div['onclick'] = function (event) {
                    event.vm = scope;
                    event.lastClick = scope._map._lastClick;
                    scope._onClick(event);

                    if(scope._map._lastClick == scope){
                        scope._map._lastClick = undefined;
                    }
                    else {
                        scope._map._lastClick = scope;
                    }
                };
            }
        };

        map.getPanes().labelPane.appendChild(div);

        return div;
    };

    ViewModel.prototype.draw = function(){
        var scope = this;

        var map = scope._map;
        var pixel = map.pointToOverlayPixel(this._point);

        scope._div.style.left = pixel.x  - scope._img.width/2 + scope._xOffset +  "px";
        scope._div.style.top  = pixel.y  - scope._img.height + scope._yOffset + "px";
    };

    ViewModel.prototype.getRecord = function () {
      return this._record;
    };

    ViewModel.prototype.setIcon = function (icon) {
        this._img.src = icon;
    };

    return ViewModel;
});
